import { reactive, readonly } from 'vue'
import { UserService } from '@/services/userService'
import { RecordModel } from '@/models/recordModel'
export const useRecordsStore = () => {
    const state = reactive({
        records: [],
        isZero: false,
        uuid: '',
    })
    const actions = {
        async loadRecords(uuid) {
            state.uuid = uuid;
            const response = await UserService.getRecords(uuid)
            console.log(response)
            if (response.status == 201) {
                state.isZero = true;
            } else {
                state.records = response.data.map((record) => {
                    return RecordModel.fromApi(record)
                })
            }
            return new Promise((resolve, reject) => {

                resolve();

            });
        },
        async addRecord(data, loginUUID, token) {
            data.uuid = state.uuid;
            data.amount *= 100;
            await UserService.addRecord(data, loginUUID, token);
        },
        async updateRecord(data, loginUUID, token) {
            data.amount *= 100;
            const response = await UserService.updateRecord(data, loginUUID, token);
            console.log(response);
            return new Promise((resolve, reject) => {
                resolve();
            });
        }
    }
    return {
        state: readonly(state),
        actions
    }
}